package com.yf.zzmusic.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencentcloudapi.wedata.v20210820.models.CommonIdOpsDto;
import com.yf.zzmusic.common.Constants;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.common.Result;
import com.yf.zzmusic.pojo.dto.comments.CommentsPageDTO;
import com.yf.zzmusic.pojo.vo.comments.CommentListVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.yf.zzmusic.service.ICommentsService;
import com.yf.zzmusic.pojo.entity.Comments;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 评论表	主要逻辑	user_id 和 type 和 target_id 不可同时一样		 前端控制器
 * </p>
 *
 * @author
 * @since 2024-03-07
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {
  @Resource
  private ICommentsService commentsService;

  @PostMapping("/insert")
  public Result save(@Validated @RequestBody Comments comments) {
    boolean result = commentsService.save(comments);
    if (result) {
      return Result.success("新增成功", null);
    } else {
      return Result.error("新增失败");
    }
  }

  @DeleteMapping("/{id}")
  public Result delete(@PathVariable Integer id) {
    boolean result = commentsService.removeById(id);
    if (result){
      return Result.success("删除成功",null);
    }else{
      return Result.error(Constants.NO_CONTENT,"删除失败");
    }
  }

  @GetMapping("/{id}")
  public Result findOne(@PathVariable Integer id) {
    Comments comments=commentsService.getById(id);
    if (comments!=null){
      return Result.success("查询成功",comments);
    }else{
      return Result.error(Constants.NO_CONTENT,"查询失败");
    }
  }

  @GetMapping("/page")
  public Result findPage(@Validated CommentsPageDTO commentsPageDTO) {
    PageInfo<Comments> pageInfo=commentsService.commentsByPage(commentsPageDTO);
    if (pageInfo.getTotal()>0){
      return Result.success("查询成功",pageInfo);
    }else{
      return Result.error(Constants.NO_CONTENT,"列表为空");
    }
  }

  @PostMapping("/list")
  public Result findCommentList(@Validated @RequestBody  CommentsPageDTO commentsPageDTO){
    PageInfo<CommentListVO> pageInfo=commentsService.commentListByPage(commentsPageDTO);
    if (pageInfo.getTotal()>0){
      return Result.success("查询成功",pageInfo);
    }else{
      return Result.error(Constants.NO_CONTENT,"列表为空");
    }
  }

}

